package cn.edu.mju.property.web.filter;

import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/*@WebFilter("/*")*/
public class LoginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();

        // 放行的资源
        if(uri.contains("/login_bak.html") || uri.contains("/LoginServlet") || uri.contains("/css/")
                || uri.contains("/fonts/")|| uri.contains("/images/") || uri.contains("/js/")|| uri.contains("/lib/")
        ){
            chain.doFilter(req, resp);
        }else {

            Object user =  request.getSession().getAttribute("user");
            if(user != null){
                chain.doFilter(req, resp);
            }else {
                request.getRequestDispatcher("/login_bak.html").forward(request,resp);
            }
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
